package net.wwang.open.controller;

import net.wwang.open.global.GlobalTipMsg;
import net.wwang.open.global.ResponseResult;
import net.wwang.open.pojo.Document;
import net.wwang.open.service.DocumentService;
import net.wwang.open.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * @author WangWei
 * @date 2020/8/11
 */
@Api(tags = {"文档接口"})
@Slf4j
@RestController
@RequestMapping("/doc")
public class DocumentPortalApi {
	@Autowired
	private DocumentService documentService;
	@Autowired
	private FileService fileService;

	/**
	 * 新增文档
	 *
	 * @param space
	 * @param parentId
	 * @param document
	 * @return
	 * @throws IOException
	 */
	@ApiOperation(value = "新增文档")
	@PostMapping(value = "/{space}/{parentId}", produces = "application/json;charset=UTF-8")
	public ResponseResult addDocument(@PathVariable("space") String space,
                                      @PathVariable("parentId") String parentId,
                                      @ApiParam(value = "请求参数：{\"title\":\" \",\"content\":\" \"}", required = true)
	                                  @Valid @RequestBody Document document) throws IOException {
		return documentService.addDocument(space, parentId, document);
	}

	/**
	 * 删除文档
	 *
	 * @param menuId
	 * @return
	 */
	@ApiOperation(value = "删除文档")
	@DeleteMapping("/{menuId}")
	public ResponseResult deleteDocument(@PathVariable("menuId") String menuId) {
		return documentService.delDocument(menuId);
	}

	/**
	 * 修改文档
	 *
	 * @param menuId
	 * @param document
	 * @return
	 * @throws IOException
	 */
	@ApiOperation(value = "修改文档")
	@PutMapping("/{menuId}")
	public ResponseResult updateDocument(@PathVariable("menuId") String menuId, @ApiParam(value = "请求参数：{\"title\":\" \",\"content\":\" \"}", required = true) @RequestBody Document document) throws IOException {
		return documentService.updateDocument(menuId, document);
	}

	/**
	 * 文档详情
	 *
	 * @param menuId
	 * @return
	 */
	@ApiOperation(value = "获取文档详情")
	@GetMapping("/detail/{menuId}")
	public ResponseResult getDocument(@PathVariable("menuId") String menuId) {
		return documentService.getDocDetail(menuId);
	}

	/**
	 * 上传文档
	 *
	 * @param menuId
	 * @param space
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@ApiOperation(value = "上传文档")
	@PostMapping("/upload/{menuId}/{space}")
	public ResponseResult uploadDocument(@PathVariable("menuId") String menuId, @PathVariable("space") String space, @RequestParam("file") MultipartFile file) {
		try {
			Document document = fileService.generateDocument(file);
			return documentService.addDocument(space, menuId, document);
		}catch (IOException e){
			return ResponseResult.FAILED(GlobalTipMsg.DOCUMENT_SAVE_EXCEPTION);
		}
	}

	/**
	 * 移动文档到新目录下
	 *
	 * @param documentId
	 * @param menuId
	 * @return
	 */
	@ApiOperation(value = "移动文档到新目录下")
	@PostMapping("/move/{menuId}/{documentId}")
	public ResponseResult moveDocToNewMenu(@PathVariable("documentId") String documentId, @PathVariable("menuId") String menuId) throws IOException {
		return documentService.moveDocument(documentId, menuId);
	}

	@ApiOperation(value = "复制文档到新目录下")
	@PostMapping("/copy/{menuId}/{documentId}")
	public ResponseResult copyDocToNewMenu(@PathVariable("documentId") String documentId, @PathVariable("menuId") String menuId) throws IOException {
		//如果目录是当前目录 重命名
		return documentService.copyDocument(menuId, documentId);
	}

	/**
	 * 导出单个文档
	 *
	 * @param documentId
	 * @param type
	 * @param response
	 * @throws IOException
	 * @throws DocumentException
	 */
	@ApiOperation(value = "导出文档")
	@PostMapping(value = "/report/{type}/{documentId}", headers = "Accept=application/octet-stream;charset=utf-8")
	public void reportDocument(@PathVariable("documentId") String documentId, @ApiParam(value = "参数值：pdf", required = true) @PathVariable("type") String type, HttpServletResponse response) throws IOException{
		documentService.reportDocument(documentId, type, response);
	}

	/**
	 * 导出多个文档
	 *
	 * @param documentIds
	 * @param type
	 * @param response
	 * @throws IOException
	 * @throws DocumentException
	 */
	@ApiOperation(value = "导出多个文档")
	@PostMapping(value = "/report/{type}", headers = "Accept=application/octet-stream;charset=utf-8")
	public void reportDocument(@RequestParam("documentIds") String[] documentIds, @ApiParam(value = "参数值：pdf ,word", required = true) @PathVariable("type") String type, HttpServletResponse response) throws IOException {
		documentService.reportDocuments(documentIds, type, response, "OpenDocs");
	}

}
